/**
 * 违规标记窗体
 */
Ext.define('Module.wg.WgEditWindow', {
	extend : 'Ext.window.Window',
	layout: 'border',
	requires: ['Sh.ux.SignPanel'],
	border: false,
	bodyPadding: 3,
	title: '操作违规标记',
	config: {
		player: undefined,
		currentTime:'',
		vdata: undefined
	},
	initComponent : function() {
		var me = this;
		me.defaults={
				split:true
		};
		var westPanel = me.westPanel=Ext.create('Ext.panel.Panel',{
			layout:'column',
			region: 'east',
			title:"违规描述",
			width:260,
//			collapsible:true,
//			collapsed:false,
			defaults: {
				border:false
			},
			tbar: [{
				text: '保存',
				iconCls:'icon-b_save',
				id:me.id+'saveBut',
				handler: function(){
					Ext.getCmp(me.id+"-draw").setValue(eastPanel.getImgeData());
					me.addBj();
				}
			},{
				text: '取消',
				iconCls:'icon-b_qx',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: [{
				xtype: 'form',
				bodyPadding: 10,
				defaults: {
					labelWidth: 60,
					style:"margin-top:15",
					anchor: '100%'
				},
				items: [{
					name: 'draw',
					id:me.id+"-draw",
					xtype: 'hidden'
				},{
					name: 'wgtime',
					xtype: 'hidden',
					value:me.player.getCurrentTime()
				},{
					name: 'videoId',
					xtype: 'hidden',
					value:me.vdata.id
				},{
					xtype: 'combobox',
					fieldLabel: '典型违规',
					name:'wgDxwgId',
					store: Ext.create('Ext.data.Store',{
						autoLoad: true,
						fields: ['id','dxwgName','describe'],
				        proxy: {
				            type: 'ajax',
				            url: 'wgVideoAction/getWgDxwgList.do'
				        }
					}),
				    queryMode: 'local',
				    displayField: 'dxwgName',
				    valueField: 'id',
				    listeners: {
				    	select:function(combo,record,opts){
				    		Ext.getCmp(me.id+'describe').setValue(record[0].get('describe'));
				    		Ext.getCmp(me.id+'title').setValue(record[0].get('dxwgName'));
				    	}
				    }
				},{
					xtype: 'textfield',
					style:"margin-top:30",
					fieldLabel: '违规名称',
					name: 'title',
					id:me.id+'title',
					maxLength:40,
					allowBlank: false
				},{
					fieldLabel: '发生时间',
					xtype: 'displayfield',
					value:me.formatSeconds(me.player.getCurrentTime())
				},{
					fieldLabel: '详细描述',
					name: 'describe',
					xtype: 'textarea',
					maxLength:100,
					id:me.id+'describe',
					allowBlank: false
				}]
			}]
		});
		
		var eastPanel =  Ext.create('Sh.ux.SignPanel',{
			region: "center",
			title: 'center',
			player: me.player,
			vdata: me.vdata
		});
		me.formatSeconds(me.player.getCurrentTime());

		me.items = [westPanel,eastPanel];

		me.callParent();
	},formatSeconds : function(value){//将秒转换为时间
		var theTime = parseInt(value);// 秒
		var theTime1 = 0;// 分
		var theTime2 = 0;// 小时
	    if(theTime > 60) {
	        theTime1 = parseInt(theTime/60);
	        theTime = parseInt(theTime%60);
	            if(theTime1 > 60) {
	            theTime2 = parseInt(theTime1/60);
	            theTime1 = parseInt(theTime1%60);
	            }
	    }
        var result = ""+parseInt(theTime);
        if(theTime1 > 0) {
        	result = ""+parseInt(theTime1)+":"+result;
        }
        if(theTime2 > 0) {
        	result = ""+parseInt(theTime2)+":"+result;
        }
       var r= result.indexOf(":");
        if(r==-1){
        	result="0:"+result;
        }
        return result;
    },
    /**
     * 根据违规标记时间和视频id删除违规标记
     */
    deleteWgBj: function(){
		var me = this;
		Ext.Msg.confirm('提示', '该时间段,违规标记已存在,是否替换？',function(b){
			if('yes' == b){
				var videoIdArr=[];
					videoIdArr.push(me.vdata.id);
				Ext.Ajax.request({
				    url: 'wgVideoAction/deleteWgBjBatch.do',
				    params: {
				    	videoIdArr : videoIdArr,
				    	wgtime : me.player.getCurrentTime(),
				    	addByStr:'addByStr'
				    },
				    success: function(response){
    	    	    	var rs = Ext.decode(response.responseText);
    	    	    	if(rs.rst){
    	    	    		me.addBj();
    	    	    	}else{
    	    	    		return Ext.Msg.alert('提示', '删除失败！');
    	    	    	}
				    }
				});
			}
		});
	},
	addBj : function(){
		var me = this;
		Ext.getCmp(me.id+'saveBut').setDisabled(true);
//		var formp = me.westPanel.up('panel').items.items[0];
    	var form = me.down('form').getForm();
    	if(form.isValid()){
    		form.submit({
	    	    url: 'wgVideoAction/addBj.do',
	    	    success: function(form, action) {
	    	    	var rs = Ext.decode(action.response.responseText);
	    	    	if(rs.rst){
	    	    		Ext.Msg.alert('提示', '保存成功！');
	    	    		var wgvp = Ext.getCmp('wgVideoPanel');
	    	    		if(wgvp){
	    	    			wgvp.store.reload();
	    	    		}
	    	    	}
	    	    	else{
	    	    		
	    	    		if(rs.msg=='已存在'){
	    	    			me.deleteWgBj();
	    	    		}else{
	    	    			Ext.Msg.alert('提示', '保存失败');
	    	    		}
	    	    	}
	    	    	
	    	    	Ext.getCmp(me.id+'saveBut').setDisabled(false);
	    	    },
	    	    failure: function(){
	    	    	Ext.getCmp(me.id+'saveBut').setDisabled(false);
	    	    	Ext.Msg.alert('提示', '保存失败,请检查你的网络。');
	    	    }
	    	});
    	}
    	else{
    		Ext.getCmp(me.id+'saveBut').setDisabled(false);
    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
    	}
	}
});
